<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
//require_once CLASS_EX_REALDIR . 'page_extends/mypage/LC_Page_AbstractMypage_Ex.php';
require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
/**
 * MyPage のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_Mypage.php 21981 2012-07-28 14:54:37Z Seasoft $
 */
class LC_Page_MyPage extends LC_Page_Ex {

    // {{{ properties

    /** ページナンバー */
    var $tpl_pageno;

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
       parent::init();
	    	
       //权限验证
       if($this->objSess->GetSession('cert')!= CERT_STRING){
       		SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'is not a certificate authority.');
       }
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
        $this->action();
        $this->sendResponse();
    }

    /**
     * Page のAction.
     *
     * @return void
     */
    function action() {
    	
		$userId = $this->getValFromPostOrGet("userId");
        
		switch ($this->getMode()) {
        	
            case 'showUserInfo':
            	
            	$this->checkUserIdIsNull($userId);
            	//去确认画面
                $this->tpl_mainpage = 'mypage/userInfoConfirm.tpl';
                $this->getUserInfoFromDB($userId);
                break;

            case 'edit':
            	$this->checkUserIdIsNull($userId);
            	$this->tpl_mainpage = 'mypage/userInfoUpdate.tpl';
            	$this->tpl_javascript = '$("#mainBody").removeClass("front");';
        		$this->tpl_javascript .= ' $("#mainBody").addClass("not-front page-user-register section-user cron-check-processed");';
        		$this->link_javascript[] = "js/sapa/regist.js";
        		
        		 
		        $this->linkCss[] = 'css/datepickerImport.css';
		        $this->link_javascript[] = "js/sapa/jquery.ui.core.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.datepicker.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.widget.js";
    
            	$this->getUserInfoFromDB($userId);
                break;
                
            case 'block':
            	$this->checkUserIdIsNull($userId);
            	$updataArr = array();
            	$updataArr['status'] = "3";
            	$objQuery = SC_Query::getSingletonInstance();
            	$objQuery->update("mst_users", $updataArr,"id = ?",array($userId));
            	
            	SC_Response_Ex::sendRedirect(HTTP_URL."mypage/index.php");
                break;
                
            case 'active':
            	$this->checkUserIdIsNull($userId);
            	$updataArr = array();
            	$updataArr['status'] = "2";
            	$objQuery = SC_Query::getSingletonInstance();
            	$objQuery->update("mst_users", $updataArr,"id = ?",array($userId));
            	
            	SC_Response_Ex::sendRedirect(HTTP_URL."mypage/index.php");
            	break;
            case 'memberAdmin':
            	 	$this->adminAuthorityCheck(AUTHORITY_MEMBER_ADMIN);
	            	$this->linkCss[] = 'css/sapaImport.css';
	            	$this->link_javascript[] = "js/sapa/jquery.dataTables.js";
	            	
	            	$this->tpl_mainpage = 'mypage/admin_index.tpl';
					$objQuery = SC_Query::getSingletonInstance();
					$where = 'a.id = b.id and b.title_id = c.id ';
				    $this->userDates = $objQuery->select('*, a.id as id ','mst_users AS  a , tab_person_users AS b, mst_title AS  c ', $where);
					
				    $this->editUserDataForScreen();
            	break;
            case 'dataExport':
            	$this->linkCss[] = 'css/sapaImport.css';
		       //引入本页面javascript
			   $this->link_javascript[] = "js/sapa/jquery.dataTables.js";
		       if($this->objSess->GetSession('authority')== AUTHORITY_ADMIN){
		       	
		       		$this->tpl_mainpage = 'mypage/admin_index.tpl';
					$objQuery = SC_Query::getSingletonInstance();
					$where = 'a.id = b.id and b.title_id = c.id ';
				    $this->userDates = $objQuery->select('*, a.id as id ','mst_users AS  a , tab_person_users AS b, mst_title AS  c ', $where);
					
				    $this->editUserDataForScreen();
				    $this->dataExport();
		       }else{
			       	if($this->objSess->GetSession('cert')== CERT_STRING){
			       		$this->tpl_mainpage = 'mypage/index.tpl';
			       	}else {
			       		SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'You can not access this page');
			       	}
		       }
                break;
            default:
            	
			   $this->linkCss[] = 'css/sapaImport.css';
		       //引入本页面javascript
			   $this->link_javascript[] = "js/sapa/jquery.dataTables.js";
		       if($this->objSess->GetSession('authority')== AUTHORITY_ADMIN){
		       	
		       		$this->tpl_mainpage = 'mypage/admin_index.tpl';
					$objQuery = SC_Query::getSingletonInstance();
					$where = 'a.id = b.id and b.title_id = c.id ';
				    $this->userDates = $objQuery->select('*, a.id as id ','mst_users AS  a , tab_person_users AS b, mst_title AS  c ', $where);
					
				    $this->editUserDataForScreen();
		       }else{
			       	if($this->objSess->GetSession('cert')== CERT_STRING){
			       		$this->tpl_mainpage = 'mypage/index.tpl';
			       	}else {
			       		SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'You can not access this page');
			       	}
		       }
                break;
        }
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
    
    function checkUserIdIsNull($userId){
    	
		if(empty($userId)){
			SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'User Id Is NULL.');
		}
    }
	function getUserInfoFromDB($userId){
		
		//SelectBox 初期化
    	$masterData = new SC_DB_MasterData();
        $this->locationList = $masterData->getMasterData("mst_location");
    	$this->titleList =$masterData->getMasterData("mst_title");
    	$this->degreeList =$masterData->getMasterData("mst_degree");
    	$this->specialtiesList =$masterData->getMasterData("mst_specialties");
    	$this->stateList =$masterData->getMasterData("mst_state");
    	$this->countryList =$masterData->getMasterData("mst_country");
    	$this->authorityList =$masterData->getMasterData("mst_authority");
    	
    	
    	//radioBox 初期化
    	$this->maillingAddressList =$masterData->getMasterData("mst_mailling_type");
    	$this->membershipFeeList =$masterData->getMasterData("mst_membership_fee");
    	
		$objQuery = SC_Query::getSingletonInstance();
	    $rsMst = $objQuery->select('*','mst_users', "id = ?", array($userId));
	    $rsPtb = $objQuery->select('*','tab_person_users', "id = ?", array($userId));
	    
	    $this->regist_postVal['updateUserId'] = strtolower(trim($rsMst[0]['id']));
		$this->regist_postVal['mail'] = strtolower(trim($rsMst[0]['mail_add']));
		$this->regist_postVal['password1'] = $rsMst[0]['pass_word'];
		
		$this->regist_postVal['location'] = $rsPtb[0]['location_id'];
		$this->regist_postVal['locationVal'] = $this->locationList[$rsPtb[0]['location_id']];

		
		$this->regist_postVal['title'] = $rsPtb[0]['title_id'];
		$this->regist_postVal['titleVal'] = $this->titleList[$rsPtb[0]['title_id']];
		
		$this->regist_postVal['lastname'] = $rsPtb[0]['lastname'];
		$this->regist_postVal['firstname'] = $rsPtb[0]['firstname'];
		$this->regist_postVal['field_mi'] = $rsPtb[0]['field_mi'];
		
		$this->regist_postVal['nameinchinese'] = $rsPtb[0]['chinese_name'];
		$this->regist_postVal['nameinchineseVal'] = $this->degreeList[$rsPtb[0]['chinese_name']];
		
		$this->regist_postVal['affiliation'] = $rsPtb[0]['affiliation_employer'];
		
		$this->regist_postVal['specialties'] = $rsPtb[0]['specialties_id'];
		$this->regist_postVal['specialtiesVal'] = $this->specialtiesList[$rsPtb[0]['specialties_id']];
		
		$this->regist_postVal['keywords'] = $rsPtb[0]['key_words'];
		
		$this->regist_postVal['mailaddress'] = $rsPtb[0]['mailling_type_id'];
		$this->regist_postVal['mailaddressVal'] = $this->maillingAddressList[$rsPtb[0]['mailling_type_id']];
		
		$this->regist_postVal['street'] = $rsPtb[0]['street'];
		//$this->regist_postVal['line2'] = $rsPtb[0]['line2'];
		$this->regist_postVal['city'] = $rsPtb[0]['city'];
		
		$this->regist_postVal['state'] = $rsPtb[0]['state_id'];
		$this->regist_postVal['stateVal'] = $this->stateList[$rsPtb[0]['state_id']];
		
		$this->regist_postVal['zip'] = $rsPtb[0]['zip_cood'];
		
		$this->regist_postVal['country'] = $rsPtb[0]['country_id'];
		$this->regist_postVal['countryVal'] = $this->countryList[$arrPost['country_id']];
		
		$this->regist_postVal['tel'] = $rsPtb[0]['tel_no'];
		$this->regist_postVal['fax'] = $rsPtb[0]['fax_no'];
		
		$this->regist_postVal['mailmagazine'] = $rsMst[0]['mailmagazine_flg'];
		
		if (trim($this->regist_postVal['mailmagazine']) == MAILMAGA_AGREE_KEY) {
			$this->regist_postVal['mailmagazineVal'] = MAILMAGA_AGREE_VALUE;
			$this->regist_postVal['mailmagazine'] = MAILMAGA_AGREE_KEY;
		}elseif ($this->regist_postVal['mailmagazine'] == MAILMAGA_DISAGREE_KEY){
			$this->regist_postVal['mailmagazineVal'] = MAILMAGA_DISAGREE_VALUE;
			$this->regist_postVal['mailmagazine'] = MAILMAGA_DISAGREE_KEY;
		}
		$this->regist_postVal['mfee'] = $rsMst[0]['member_flg'];
		$this->regist_postVal['mfeeVal'] = $this->membershipFeeList[$rsMst[0]['member_flg']];
		$this->regist_postVal['authority'] =$rsMst[0]['authority_flg'];
		$this->regist_postVal['authorityVal'] =$this->authorityList[$rsMst[0]['authority_flg']];
		$this->regist_postVal['endDate'] =$rsMst[0]['paymented_date'];
	}
	
	function editUserDataForScreen(){
		
		$masterData = new SC_DB_MasterData();
    	//radioBox 初期化
    	$membershipFeeList =$masterData->getMasterData("mst_membership_fee");
    	$authorityList = $masterData->getMasterData("mst_authority");
		
    	foreach ($this->userDates as $index => $value) {
			
			//付费用户类型编辑
			$this->userDates[$index]['member_flg'] = $membershipFeeList[$value['member_flg']];
			$authority = $authorityList[$value['authority_flg']];
			if(!empty($authority)){
				$this->userDates[$index]['authority'] = $authority;
			}else{
				$this->userDates[$index]['authority'] = "Unkonw";
			}
			//用户状态编辑
			if ($value['status'] == "2") {
				$this->userDates[$index]['status'] = "Normal";
			}elseif ($value['status'] == "3"){
				$this->userDates[$index]['status'] = "Blocked";
			}else {
				$this->userDates[$index]['status'] = "Others";
			}
		}
	}
	
	function dataExport(){
	
		$csv = "";
		if(!empty($this->userDates)){
			foreach ($this->userDates as $key => $value) {
				$csv .=$value["id"]. ",";
				$csv .=$value["mail_add"]. ",";
				$csv .=$value["lastname"]." ".$value["firstname"].",";
				$csv .=$value["authority"]. ",";
				$csv .=$value["paymented_date"]. ",";
				$csv .=$value["member_flg"]. ",";
				$csv .=$value["status"]. "\n";
				
			}
			header("Content-Type: application/octet-stream");
			header("Content-Disposition: attachment; filename=membersList.csv");
			//header("Content-Type: text/csv; charset=UTF-8 ");
			//echo mb_convert_encoding("テスト\n","SJIS", "UTF-8");
		}
		echo($csv);
		exit();
	}
}
